﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreLib.Communication.Sockets;
using CoreLib.Communication.Packets;
using CoreLib.MySQL.Mapping;

namespace AuthServer
{
    [DbTable("account", AutoIncrementerStart = 1000000)]
    public class Client
    {
        [DbField("username", Nullable = false)]
        public string Username;
        [DbField("password", Nullable = false)]
        public string Password;
        [DbField("uid", IsPrimaryKey = true, AutoIncrement = true)]
        public uint UId;

        private SocketClient csocket;

        public Client(SocketClient _csocket)
        {
            this.csocket = _csocket;
            this.csocket.Owner = this;
            this.csocket.Cryptography = new Cryptography.AuthCryptography();
        }

        //Como es una clase propia, uso el bool para saber si luego de enviar el packet
        //se seguira usando o true si se puede reciclar
        public void Send(Packet stream, bool dispose)
        {
            try
            {
                byte[] packet = stream.ToArray();
                this.csocket.Cryptography.Encrypt(packet);
                this.csocket.Socket.BeginSend(packet, 0, packet.Length, System.Net.Sockets.SocketFlags.None, (result => csocket.Socket.EndSend(result)), null);
            }
            catch(Exception e)
            {
                throw e;
            }
        }

        public void Send(IPacket packet, bool dispose)
        {
            Send(packet.Serialize(), dispose);
        }
    }
}
